<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>AudioCD.pb</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font><font color="#3A3966">;   PureBasic - Audio CD example file
</font><font color="#3A3966">;
</font><font color="#3A3966">;    (c) Fantaisie Software
</font><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font>
NbCDDrives =<font color="#3A3966"> InitAudioCD</font>()

<b><font color="#3A3966">If</font></b> NbCDDrives = 0
<font color="#3A3966">  MessageRequester</font>("Error", "No CD Audio drives found...", 0)
  <b><font color="#3A3966">End</font></b>
<b><font color="#3A3966">EndIf</font></b>

<b><font color="#3A3966">Global</font></b> Null$

<b><font color="#3A3966">Procedure</font></b>.s <font color="#3A3966">GetHourFormat</font>(LengthInSeconds)
  Minutes = LengthInSeconds/60
  Seconds = LengthInSeconds-Minutes*60
  <b><font color="#3A3966">If</font></b> Seconds &lt; 10 : Null$ = "0" : <b><font color="#3A3966">Else</font></b> : Null$ = "" : <b><font color="#3A3966">EndIf</font></b>

  <b><font color="#3A3966">ProcedureReturn</font></b> <font color="#3A3966">Str</font>(Minutes)+":"+Null$+<font color="#3A3966">Str</font>(Seconds)
<b><font color="#3A3966">EndProcedure</font></b>

<font color="#3A3966">; Initialize constants for easier code reading
</font><font color="#3A3966">;
</font><font color="#924B72">#GADGET_Play</font>   = 0
<font color="#924B72">#GADGET_Stop</font>   = 1
<font color="#924B72">#GADGET_Eject</font>  = 2
<font color="#924B72">#GADGET_Close</font>  = 3
<font color="#924B72">#GADGET_Select</font> = 4
<font color="#924B72">#GADGET_Status</font> = 5
<font color="#924B72">#GADGET_Time</font>   = 6
<font color="#924B72">#GADGET_AudioCDDrive</font> = 7
<font color="#924B72">#GADGET_SelectDrive</font>  = 8

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">RefreshCD</font>()
<font color="#3A3966">  ClearGadgetItems</font>(<font color="#924B72">#GADGET_Select</font>)

  NbAudioTracks =<font color="#3A3966"> AudioCDTracks</font>()
  <b><font color="#3A3966">For</font></b> k=1 <b><font color="#3A3966">To</font></b> NbAudioTracks
<font color="#3A3966">    AddGadgetItem</font>(<font color="#924B72">#GADGET_Select</font>, -1, "Track "+<font color="#3A3966">Str</font>(k))
  <b><font color="#3A3966">Next</font></b>
<font color="#3A3966">  SetGadgetState</font>(<font color="#924B72">#GADGET_Select</font>, 0)
<b><font color="#3A3966">EndProcedure</font></b>


<b><font color="#3A3966">If</font></b> <font color="#3A3966">OpenWindow</font>(0, 100, 200, 265, 125, "PureBasic - AudioCD Example", <font color="#924B72">#PB_Window_SystemMenu</font> | <font color="#924B72">#PB_Window_ScreenCentered</font>)

<font color="#3A3966">  ButtonGadget  </font>(<font color="#924B72">#GADGET_Play</font>   ,  10, 10, 60 , 24, "Play")
<font color="#3A3966">  ButtonGadget  </font>(<font color="#924B72">#GADGET_Stop</font>   ,  70, 10, 60 , 24, "Stop")
<font color="#3A3966">  ButtonGadget  </font>(<font color="#924B72">#GADGET_Eject</font>  , 130, 10, 60 , 24, "Eject")
<font color="#3A3966">  ButtonGadget  </font>(<font color="#924B72">#GADGET_Close</font>  , 190, 10, 60 , 24, "Close")
<font color="#3A3966">  ComboBoxGadget</font>(<font color="#924B72">#GADGET_Select</font> ,  10, 43, 240, 24)
<font color="#3A3966">  TextGadget</font>(<font color="#924B72">#GADGET_Status</font>, 10, 70, 180, 24, "Status: stopped")
<font color="#3A3966">  TextGadget</font>(<font color="#924B72">#GADGET_Time</font>, 200, 70, 240, 24, "")

<font color="#3A3966">  TextGadget</font>(<font color="#924B72">#GADGET_AudioCDDrive</font>, 10, 99, 180, 24, "Select the CD-Audio drive :")
<font color="#3A3966">  ComboBoxGadget</font>(<font color="#924B72">#GADGET_SelectDrive</font>, 210, 95, 40, 24)

  <b><font color="#3A3966">For</font></b> k=1 <b><font color="#3A3966">To</font></b> NbCDDrives
<font color="#3A3966">    UseAudioCD</font>(k-1)
<font color="#3A3966">    AddGadgetItem</font>(<font color="#924B72">#GADGET_SelectDrive</font>, -1,<font color="#3A3966"> Left</font>(<font color="#3A3966">AudioCDName</font>(),2))
  <b><font color="#3A3966">Next</font></b>
<font color="#3A3966">  SetGadgetState</font>(<font color="#924B72">#GADGET_SelectDrive</font>, 0)
<font color="#3A3966">  UseAudioCD</font>(0)

  <b><font color="#3A3966">If</font></b> NbCDDrives = 1
<font color="#3A3966">    DisableGadget</font>(<font color="#924B72">#GADGET_SelectDrive</font>, 1)
  <b><font color="#3A3966">EndIf</font></b>

<font color="#3A3966">  RefreshCD</font>()

  <b><font color="#3A3966">Repeat</font></b>
    <b><font color="#3A3966">Repeat</font></b>
      Event =<font color="#3A3966"> WindowEvent</font>()  <font color="#3A3966">; This time we use the WindowEvent(), non-blocking command to allow time refreshing
</font>
      <b><font color="#3A3966">If</font></b> Event = <font color="#924B72">#PB_Event_Gadget</font>
        <b><font color="#3A3966">Select</font></b> <font color="#3A3966">EventGadget</font>()

          <b><font color="#3A3966">Case</font></b> <font color="#924B72">#GADGET_Play</font>
            CurrentTrack =<font color="#3A3966"> GetGadgetState</font>(4)+1
<font color="#3A3966">            PlayAudioCD</font>(CurrentTrack, CurrentTrack)
            
          <b><font color="#3A3966">Case</font></b> <font color="#924B72">#GADGET_Stop</font>
<font color="#3A3966">            StopAudioCD</font>()

          <b><font color="#3A3966">Case</font></b> <font color="#924B72">#GADGET_Eject</font>
<font color="#3A3966">            EjectAudioCD</font>(1)

          <b><font color="#3A3966">Case</font></b> <font color="#924B72">#GADGET_Close</font>
<font color="#3A3966">            EjectAudioCD</font>(0)

          <b><font color="#3A3966">Case</font></b> <font color="#924B72">#GADGET_SelectDrive</font>
<font color="#3A3966">            UseAudioCD</font>(<font color="#3A3966">GetGadgetState</font>(<font color="#924B72">#GADGET_SelectDrive</font>))
<font color="#3A3966">            RefreshCD</font>()

        <b><font color="#3A3966">EndSelect</font></b>

      <b><font color="#3A3966">Else</font></b>
        <b><font color="#3A3966">If</font></b> Event = <font color="#924B72">#PB_Event_CloseWindow</font> : Quit = 1 : <b><font color="#3A3966">EndIf</font></b>
      <b><font color="#3A3966">EndIf</font></b>
    <b><font color="#3A3966">Until</font></b> Event = 0

<font color="#3A3966">    Delay</font>(20) <font color="#3A3966">; Wait 20 ms, which is a long period for the processor, to don't steal the whole CPU power
</font>              <font color="#3A3966">; for our little application :)
</font>
    CurrentTrack =<font color="#3A3966"> AudioCDStatus</font>()
    <b><font color="#3A3966">If</font></b> CurrentTrack &gt; 0
<font color="#3A3966">      SetGadgetText</font>(<font color="#924B72">#GADGET_Status</font>, "Playing Track "+<font color="#3A3966">Str</font>(CurrentTrack)+" (Length: "+<font color="#3A3966">GetHourFormat</font>(<font color="#3A3966">AudioCDTrackLength</font>(CurrentTrack))+")")
<font color="#3A3966">      SetGadgetText</font>(<font color="#924B72">#GADGET_Time</font>, "Time: "+<font color="#3A3966">GetHourFormat</font>(<font color="#3A3966">AudioCDTrackSeconds</font>()))
<font color="#3A3966">      DisableGadget</font>(<font color="#924B72">#GADGET_Play</font>, 1)
<font color="#3A3966">      DisableGadget</font>(<font color="#924B72">#GADGET_Stop</font>, 0)
<font color="#3A3966">      DisableGadget</font>(<font color="#924B72">#GADGET_Select</font>, 0)
    <b><font color="#3A3966">Else</font></b>
<font color="#3A3966">      SetGadgetText</font>(<font color="#924B72">#GADGET_Status</font>, "Status: Stopped")
<font color="#3A3966">      SetGadgetText</font>(<font color="#924B72">#GADGET_Time</font>, "")
<font color="#3A3966">      DisableGadget</font>(<font color="#924B72">#GADGET_Play</font>, 0)
<font color="#3A3966">      DisableGadget</font>(<font color="#924B72">#GADGET_Stop</font>, 1)

      <b><font color="#3A3966">If</font></b> CurrentTrack = -1 <font color="#3A3966">; CD Drive not ready
</font><font color="#3A3966">        DisableGadget</font>(<font color="#924B72">#GADGET_Select</font>, 1)
      <b><font color="#3A3966">Else</font></b>
<font color="#3A3966">        DisableGadget</font>(<font color="#924B72">#GADGET_Select</font>, 0)
      <b><font color="#3A3966">EndIf</font></b>
    <b><font color="#3A3966">EndIf</font></b>

  <b><font color="#3A3966">Until</font></b> Quit = 1

<b><font color="#3A3966">EndIf</font></b>

<b><font color="#3A3966">For</font></b> k=0 <b><font color="#3A3966">To</font></b> NbCDDrives-1    <font color="#3A3966">; Stop all the CD drives, if some are playing together
</font><font color="#3A3966">  UseAudioCD</font>(k)
<font color="#3A3966">  StopAudioCD</font>()
<b><font color="#3A3966">Next</font></b>

<b><font color="#3A3966">End</font></b></font></pre>
</body></html>
